package com.paw.ruoyi.admin.common.domain;

import java.util.List;

import com.paw.ruoyi.admin.common.enums.CommonResponseCodeEnum;
import com.paw.ruoyi.admin.common.enums.ICommonEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author Rubble
 * @date 2021/8/14 参见：https://pro.ant.design/zh-CN/docs/request/
 **/
@Data
@ApiModel("Response")
public class Result<T> {

  public static int SUCCESS_CODE = 200;
  public static int ERROR_CODE = 500;
  public static String SUCCESS_MSG = "success";

  @ApiModelProperty("数据")
  private T data;

  @ApiModelProperty("编码")
  private int code;

  @ApiModelProperty("消息")
  private String message;

  public Result () {
  }

  public Result (int code, String message) {
    this.code = code;
    this.message = message;
  }

  public Result (int code, String message,T data) {
    this.data = data;
    this.code = code;
    this.message = message;
  }

  public static <T> Result<T> result () {
    Result<T> result = new Result<>();
    result.setCode(SUCCESS_CODE);
    result.setMessage(SUCCESS_MSG);
    return result;
  }

  public static <T> Result<T> result (T data) {
    Result<T> result = new Result<>();
    result.setCode(SUCCESS_CODE);
    result.setMessage(SUCCESS_MSG);
    result.setData(data);
    return result;
  }


  public static <T> Result<PageData<T>> page (List<T> data, Long total) {
    Result<PageData<T>> result = new Result<>();
    result.setCode(SUCCESS_CODE);
    result.setMessage(SUCCESS_MSG);
    PageData<T> page = new PageData<>(total,data);
    result.setData(page);
    return result;
  }


  public static <T> Result<T> result (ICommonEnum commonEnum) {
    Result<T> result = new Result<>();
    result.setCode(commonEnum.getCode());
    result.setMessage(commonEnum.getDesc());
    return result;
  }


  public static <T> Result<T> result (Integer code, String message, T data) {
    Result<T> result = new Result<>(code,message,data);
    result.setCode(code);
    result.setMessage(message);
    result.setData(data);
    return result;
  }

  public static <T> Result<T> result (Integer code, String message) {
    return new Result<>(code,message);
  }

  public static  Result<Boolean> resultBoolean (Boolean success) {
    Result<Boolean> result = new Result<>();
    int code = success ? 200 : 200500;
    String message = success ? "操作成功" : "操作失败";
    result.setCode(code);
    result.setMessage(message);
    result.setData(success);
    return result;
  }

  public  static <T> Result<T> success(){
    return Result.result(CommonResponseCodeEnum.SUCCESS);
  }

  public  static <T> Result<T> error(){
    return Result.result(CommonResponseCodeEnum.ERROR);
  }

  public static <T> Result<T> error(String message){
    return new Result<>(ERROR_CODE,message);
  }


  /**
   * 200 成功
   */
  public boolean isSuccess () {
    return code == 200;
  }
}
